package com.api.shihua.api;
import com.alibaba.fastjson.JSONObject;
import com.api.shihua.util.BaseUtil;
import weaver.conn.RecordSet;
import weaver.file.Prop;
import weaver.general.Util;
import weaver.hrm.User;
import org.apache.log4j.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import java.nio.charset.Charset;

@Path("/shihua")
public class getPassword {
    @POST
    @Path("/getPassword")
    @Produces({"text/plain"})
    public String getUserPasswordRom(@Context HttpServletRequest var1, @Context HttpServletResponse var2){
        JSONObject json=new JSONObject();
        String var8 = Util.null2String(var1.getParameter("userpassword"));
        String newPassword=BaseUtil.getUserPasswordByRom(var8);
        json.put("newPassword",newPassword);
        return JSONObject.toJSONString(json);
    }
    @POST
    @Path("/getLoginid")
    @Produces({"text/plain"})
    public String getLoginid(@Context HttpServletRequest var1, @Context HttpServletResponse var2){
        JSONObject json=new JSONObject();
        String loginid = Util.null2String(var1.getParameter("loginid"));
        String URL=Util.null2String(Prop.getPropValue("FR_cscec", "URL"));

        String secretKey=Util.null2String(Prop.getPropValue("FR_cscec", "secretKey"));
        String appid=Util.null2String(Prop.getPropValue("FR_cscec", "appid")).trim();
        String encryptLoginid = new BaseUtil().encrypt(loginid, Charset.forName("UTF-8"), secretKey);
        json.put("loginid",encryptLoginid);
        json.put("appid",appid);
        json.put("url",URL);
        return JSONObject.toJSONString(json);
    }
    @POST
    @Path("/getLoginid2")
    @Produces({"text/plain"})
    public String getLoginid2(@Context HttpServletRequest var1, @Context HttpServletResponse var2){
        String loginid="";
        Logger log = Logger.getLogger(JSONObject.class);
        JSONObject json=new JSONObject();
        String userid = Util.null2String(var1.getParameter("userid"));
        User user=(User)var1.getSession(true).getAttribute("weaver_user@bean");
        if(user!=null){
            userid=user.getUID()+"";
            log.error("current userid--"+userid+" "+user.getLastname());
        }else{
            log.error("current userid--null");
        }
        RecordSet re=new RecordSet();
        String sql="select  id,password,workcode,loginid from  hrmresource where id='"+userid+"' and status in (0,1,2,3)";
        re.execute(sql);
        while(re.next()){
            String id=Util.null2String(re.getString("id") ).trim();
            loginid=Util.null2String(re.getString("loginid")).trim();
            String oldpassword=Util.null2String(re.getString("password")).trim();
            if("".equals(loginid)){
                continue;
            }
            String password=Util.getEncrypt(loginid);
            if(oldpassword.equals(password)){
                continue;
            }else{
                //updPass(id,password);
            }
        }
        json.put("loginid",loginid);
        return JSONObject.toJSONString(json);
    }
    public void updPass(String id,String password){
        RecordSet re=new RecordSet();
        re.execute("update hrmresource set password='"+password+"' where id='"+id+"'");
    }
}
